<?php

namespace App\Sidong\Controllers;

use App\Models\Coach;
use App\Models\CoachOrder;
use App\Models\MemberCard;
use App\Models\MemberCardOrder;
use App\Models\User;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;

class Dashboard
{
    public static function userInfoBox()
    {
        $user_count = User::where(['tenant_id' => \Admin::user()->tenant_id])->count();
        $infoBox = new InfoBox('用户总数', 'users', 'aqua', admin_url('users'), $user_count);
        return $infoBox->render();
    }

    public static function memberInfoBox()
    {
        $member_count = User::where(['tenant_id' => \Admin::user()->tenant_id, 'is_member' => true])->count();
        $infoBox = new InfoBox('会员总数', 'user-plus', 'green', admin_url('members'), $member_count);
        return $infoBox->render();
    }

    public static function coachInfoBox()
    {
        $coach_count = Coach::where(['tenant_id' => \Admin::user()->tenant_id, 'type' => Coach::TYPE_PRIVATE_COACH, 'is_enable' => true])->count();
        $infoBox = new InfoBox('私教总数', 'male', 'yellow', admin_url('coaches'), $coach_count);
        return $infoBox->render();
    }

    public static function cardInfoBox()
    {
        $card_count = MemberCard::where(['tenant_id' => \Admin::user()->tenant_id, 'is_enable' => true])->count();
        $infoBox = new InfoBox('已上架会员卡', 'id-card', 'red', admin_url('member-cards'), $card_count);
        return $infoBox->render();
    }

    public static function cardOrderInfoBox()
    {
        $card_order_sum = MemberCardOrder::where(['tenant_id' => \Admin::user()->tenant_id, 'is_paid' => true])->sum('total_fee');
        $infoBox = new InfoBox('会员卡订单收入', 'line-chart', 'purple', admin_url('member-card-orders'), "¥{$card_order_sum}");
        return $infoBox->render();
    }

    public static function coachOrderInfoBox()
    {
        $coach_order_sum = CoachOrder::where(['tenant_id' => \Admin::user()->tenant_id, 'is_paid' => true])->sum('total_fee');
        $infoBox = new InfoBox('私教订单收入', 'universal-access', 'blue', admin_url('member-card-orders'), "¥{$coach_order_sum}");
        return $infoBox->render();
    }

    public static function orderLineChart()
    {
        $orders = [];
        $prefix = config('admin.route.prefix');
        $template = "admin.{$prefix}.home.order-line-chart";
        $chart = 'orderLineChart';
        $html=<<<EOF
		<div id="orderLineChart" style="width: 100%;height:100%;">加载中....</div>
EOF;
        $order_line_chart = view($template, compact('orders'))->with(['chart_html' => $html, 'chart' => $chart]);
        $box = new Box('2020年度订单销售趋势', $order_line_chart);
        $box->solid();
        $box->style('order-line-chart-box');
        \Admin::style('.box-order-line-chart-box .box-body{height: 400px}');
        return $box->render();
    }

    public static function newMemberLineChart()
    {
        $members = [];
        $prefix = config('admin.route.prefix');
        $template = "admin.{$prefix}.home.new-member-line-chart";
        $chart = 'newMemberLineChart';
        $html=<<<EOF
		<div id="newMemberLineChart" style="width: 100%;height:100%;">加载中....</div>
EOF;
        $order_line_chart = view($template, compact('members'))->with(['chart_html' => $html, 'chart' => $chart]);
        $box = new Box('新会员入驻趋势', $order_line_chart);
        $box->solid();
        $box->style('new-member-line-chart-box');
        \Admin::style('.box-new-member-line-chart-box .box-body{height: 200px}');
        return $box->render();
    }

    public static function coachTopTable()
    {
        $headers = ['排名', '姓名', '销售额'];
        $lists = Coach::where(['type' => Coach::TYPE_PRIVATE_COACH, 'is_enable' => true])
            ->select(['id', 'realname'])
            ->limit(9)
            ->get()->each(function ($item) use (&$i) {
                $item->total_fee = $item->orderSum('month');
            });
        $rows = [];
        foreach ($lists as $key => $list) {
            $rows[] = [
                $key + 1,
                $list->realname,
                '¥' . $list->total_fee,
            ];
        }
        $table = new Table($headers, $rows);
        $box = new Box('本月教练销售排名', $table);
        $box->style('coach-top-box');
        $box->solid();
        \Admin::style('.box-coach-top-box .box-body{height: 400px}');
        return $box->render();
    }
}
